import pygame
import msg
import random

STARNUM = 80
MAXOBJ = 90
GUNFIRETOP = 130
TOTALFIRETOP = 140

class pcounter:
    def __init__(self):
        self.score = 0

class cplayer:
    def __init__(self, playernum, screen, screen_width, screen_height, objlist, mid = True):

        self.screen = screen
        self.screen_width = screen_width
        self.screen_height = screen_height
        self.objlist = objlist
        self.pcnt   = pcounter()
        self.playernum = playernum
        self.active = True
        
        if (playernum == 1):
            self.image = pygame.image.load("player1.png")
            self.imagerect = self.image.get_rect()
            if (mid):
                self.imagerect = self.imagerect.move(self.screen_width / 2 - self.imagerect.width / 2, self.screen_height - self.imagerect.height)
            else:
                self.imagerect = self.imagerect.move(self.screen_width / 4, self.screen_height - self.imagerect.height)
            self.score   = msg.msgimage("SCORE: " + str(self.pcnt.score),(0,0))#player score data
        elif (playernum == 2):
            self.image = pygame.image.load("player2.png")
            self.imagerect = self.image.get_rect()
            if (mid):
                self.imagerect = self.imagerect.move(self.screen_width / 2 - self.imagerect.width / 2, self.screen_height - self.imagerect.height)
            else:
                self.imagerect = self.imagerect.move(3 * self.screen_width / 4, self.screen_height - self.imagerect.height)
            self.score   = msg.msgimage("SCORE: " + str(self.pcnt.score),(self.screen_width - 180,0))#player score data
            
        self.up     = 0 #going up
        self.down   = 0 #going down
        self.left   = 0 #going left
        self.right  = 0 #going right
        self.fire   = 0 #going to fire
        self.v      = 8 #speed
        self.alive = True

    def set_move_left(self):
        self.left = True

    def set_move_right(self):
        self.right = True

    def set_move_up(self):
        self.up = True

    def set_move_down(self):
        self.down = True

    def set_move_fire(self):
        self.fire = True

    def clr_move_left(self):
        self.left = False

    def clr_move_right(self):
        self.right = False

    def clr_move_up(self):
        self.up = False

    def clr_move_down(self):
        self.down = False

    def clr_move_fire(self):
        self.fire = False

    def fireinthehole(self, target, belongto = None):
        for i in range(MAXOBJ, GUNFIRETOP):
            if (self.objlist[i].type == self.objlist[i].NON_TYPE):
                self.objlist[i].set_type(self.objlist[i].GUN_FIRE, target, belongto)
                return

    def motion(self):
        if (self.alive and self.active):
            if (self.up):
                if (self.imagerect.top - self.v >= 0):
                    self.imagerect = self.imagerect.move(0,-self.v)
                else:
                    self.imagerect = self.imagerect.move(0,0 - self.imagerect.top)
            if (self.down):
                if (self.imagerect.bottom + self.v < self.screen_height):
                    self.imagerect = self.imagerect.move(0,self.v)
                else:
                    self.imagerect = self.imagerect.move(0,self.screen_height - self.imagerect.bottom)
            if (self.left):
                if (self.imagerect.left - self.v >= 0):
                    self.imagerect = self.imagerect.move(-self.v,0)
                else:
                    self.imagerect = self.imagerect.move(0 - self.imagerect.left,0)
            if (self.right):
                if (self.imagerect.right + self.v < self.screen_width):
                    self.imagerect = self.imagerect.move(self.v,0)
                else:
                    self.imagerect = self.imagerect.move(self.screen_width - self.imagerect.right,0)
            if (self.fire):
                self.fireinthehole(self.imagerect,self)

    def show(self):
        if (self.alive and self.active):
            self.screen.blit(self.image,self.imagerect)

    def updatescore(self):
        if (self.alive):
            self.score.update("SCORE: " + str(self.pcnt.score))
        else:
            self.score.update("OH!!  SHIT!!!")
        if (self.active):
            self.score.show(self.screen)
